package gui_layer;

import java.awt.Point;
import java.util.List;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

import model_layer.Customer;
import ctr_layer.CustomerCtr;

@SuppressWarnings("serial")
public class ManageCustomersGUI extends UniversalManageWindow
{
	private List<Customer> all_customers;
	private CustomerCtr cust_ctr;
	protected String[] column_names = { "ID", "Name", "Phone number", "Email", "Address", "Zipcode", "City",  "Discount %" };

	/**
	 * Create the frame.
	 */
	public ManageCustomersGUI()
	{
		cust_ctr = new CustomerCtr();
		setTitle("Manage Customers");
		prepare_table(new Object[0][8], column_names);
		get_non_deleted_customers();
		setBounds(100, 100, 700, 350);
		setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
		customize_search();
		customize_box();
		this.addInternalFrameListener(new InternalFrameAdapter()
		{
			@Override
			public void internalFrameActivated(InternalFrameEvent arg0)
			{
				dynamically_search();
				
			}
		});
	}
	
	private void customize_search()
	{
		search_lbl.setText("Name/ID");
		
	}
	
	private void customize_box()
	{
		show_deleted_chckbox.setText("deleted customers");
	}

	@Override
	protected void dynamically_search()
	{
		String text = search_field.getText().toLowerCase();
		MyTableModel mtm = (MyTableModel) table.getModel();
		Vector<Object[]> vector = new Vector<Object[]>();
		Object[] data;
		for(Customer cust : all_customers)
		{
			if(!cust.getId().equals("0"))
			{
				if(cust.getName().toLowerCase().startsWith(text) || cust.getId().startsWith(text))
				{
					
					data = new Object[8];
					data[0] = Integer.parseInt(cust.getId());
					data[1] = cust.getName();
					data[2] = cust.getPhone_nr();
					data[3] = cust.getEmail();
					data[4] = cust.getAddress();
					data[5] = cust.getZipcode();
					data[6] = cust.getCity();
					data[7] = cust.getDiscount();
					vector.add(data);
				}
			}
		}
		Object[][] filling = new Object[vector.size()][8];
		for(int i = 0; i < vector.size(); i++)
		{
			filling[i] = vector.get(i);
		}
		mtm.setDataVector(filling, column_names);
	}
	
	private void get_non_deleted_customers()
	{
		all_customers = cust_ctr.find_non_deleted_customers();
	}
	
	private void get_deleted_customers()
	{
		all_customers = cust_ctr.find_deleted_customers();
	}

	@Override
	protected void show_deleted()
	{
		get_deleted_customers();
		dynamically_search();
		
	}

	@Override
	protected void show_non_deleted()
	{
		get_non_deleted_customers();
		dynamically_search();
		
	}

	@Override
	protected void create_new()
	{
		NewCustomerGUI nc_gui = new NewCustomerGUI(null, false);
		this.getDesktopPane().add(nc_gui);
		Point window_corner = this.getLocation();
		nc_gui.setLocation((int) window_corner.getX() + 15, (int) window_corner.getY() + 15);
		nc_gui.setVisible(true);
		nc_gui.addInternalFrameListener(new InternalFrameAdapter()
		{
			@Override
			public void internalFrameClosing(InternalFrameEvent arg0)
			{
				dynamically_search();
			}
		});
		
	}

	@Override
	protected void edit()
	{
		int row = table.getSelectedRow();
		if(row != -1)
		{
			row = table.convertRowIndexToModel(row);
			String id = String.valueOf(table.getModel().getValueAt(row, 0));
			NewCustomerGUI nc_gui = new NewCustomerGUI(cust_ctr.find_customer_by_id(id), true);
			this.getDesktopPane().add(nc_gui);
			Point window_corner = this.getLocation();
			nc_gui.setLocation((int) window_corner.getX() + 15, (int) window_corner.getY() + 15);
			nc_gui.setVisible(true);
			nc_gui.addInternalFrameListener(new InternalFrameAdapter()
			{
				@Override
				public void internalFrameClosing(InternalFrameEvent arg0)
				{
					dynamically_search();
				}
			});
		}
		
	}

	@Override
	protected void view()
	{
		int row = table.getSelectedRow();
		if(row != -1)
		{
			row = table.convertRowIndexToModel(row);
			String id = String.valueOf(table.getModel().getValueAt(row, 0));
			NewCustomerGUI nc_gui = new NewCustomerGUI(cust_ctr.find_customer_by_id(id), false);
			this.getDesktopPane().add(nc_gui);
			Point window_corner = this.getLocation();
			nc_gui.setLocation((int) window_corner.getX() + 15, (int) window_corner.getY() + 15);
			nc_gui.setVisible(true);
		}
		
	}

	@Override
	protected void delete()
	{
		int row = table.getSelectedRow();
		if(row != -1)
		{
			row = table.convertRowIndexToModel(row);
			int id = (int) table.getModel().getValueAt(row, 0);
			if(JOptionPane.showConfirmDialog(this, "Are you sure you wish to delete this customer? This can be reversed.",
					"Delete customer", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
			{
				if(cust_ctr.delete_customer(id))
				{
					JOptionPane.showMessageDialog(this, "Customer successfully deleted!", "Success", JOptionPane.PLAIN_MESSAGE);
					show_non_deleted();
				}
				else JOptionPane.showMessageDialog(this, "Error!\nCustomer wasn't deleted", "Error", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	@Override
	protected void restore()
	{
		int row = table.getSelectedRow();
		if(row != -1)
		{
			row = table.convertRowIndexToModel(row);
			int id = (int) table.getModel().getValueAt(row, 0);
			if(JOptionPane.showConfirmDialog(this, "Are you sure you wish to restore this customer?",
					"Delete customer", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
			{
				if(cust_ctr.restore_customer(id))
				{
					JOptionPane.showMessageDialog(this, "Customer successfully restored!", "Success", JOptionPane.PLAIN_MESSAGE);
					show_deleted();
				}
				else JOptionPane.showMessageDialog(this, "Error!\nCustomer wasn't restored", "Error", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

}
